package com.quiz.bis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.quiz.bis.domain.model.QuestionType;
import com.quiz.bis.mapper.QuestionTypeMapper;
import com.quiz.bis.service.IQuestionTypeService;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.utils.StringUtils;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 题目类型服务层实现
 *
 * @author ahuan
 */
@Service
@RequiredArgsConstructor
public class QuestionTypeServiceImpl implements IQuestionTypeService {

    private final QuestionTypeMapper baseMapper;

    @Override
    public TableDataInfo<QuestionType> page(QuestionType questionType, PageQuery page) {
        LambdaQueryWrapper<QuestionType> lqw = buildQueryWrapper(questionType);
        return TableDataInfo.build(baseMapper.selectPage(page.build(), lqw));
    }

    @Override
    public List<QuestionType> list(QuestionType questionType) {
        LambdaQueryWrapper<QuestionType> lqw = buildQueryWrapper(questionType);
        return baseMapper.selectList(lqw);
    }

    @Override
    public List<QuestionType> selectAll() {

        return baseMapper.selectList();
    }

    @Override
    public QuestionType getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean save(QuestionType questionType) {
        return baseMapper.insert(questionType) > 0;
    }

    @Override
    public boolean update(QuestionType questionType) {
        return baseMapper.updateById(questionType) > 0;
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids)) > 0;
    }

    private LambdaQueryWrapper<QuestionType> buildQueryWrapper(QuestionType questionType) {
        LambdaQueryWrapper<QuestionType> lqw = Wrappers.lambdaQuery();
        lqw.eq(questionType.getId() != null, QuestionType::getId, questionType.getId());
        lqw.like(StringUtils.isNotBlank(questionType.getName()), QuestionType::getName, questionType.getName());
        lqw.eq(StringUtils.isNotBlank(questionType.getCode()), QuestionType::getCode, questionType.getCode());
        lqw.eq(questionType.getHasOption() != null, QuestionType::getHasOption, questionType.getHasOption());
        lqw.eq(questionType.getHasAnswer() != null, QuestionType::getHasAnswer, questionType.getHasAnswer());
        lqw.eq(questionType.getHasFile() != null, QuestionType::getHasFile, questionType.getHasFile());
        lqw.eq(QuestionType::getDelFlag, 0);
        return lqw;
    }
}